package com.compass.doctor.ui.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.model.AddBankCard
import com.compass.framework.model.BankCard
import com.compass.framework.model.Wallet
import com.compass.framework.model.WalletDetail
import com.compass.framework.model.WithdrawType
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 积分ViewModel
 * Date:2023/11/17 14:43
 */
class IntegralViewModel : BaseViewModel() {
    //积分信息
    val walletLiveData = MutableLiveData<Wallet?>()
    val walletListLiveData = MutableLiveData<MutableList<WalletDetail>?>()
    val walletDrawTypeLiveData = MutableLiveData<MutableList<WithdrawType>?>()
    val walletDetailsListLiveData = MutableLiveData<WalletDetail?>()

    val addCardLiveData = MutableLiveData<Boolean>()
    val deleteCardLiveData = MutableLiveData<Boolean>()
    val careNumberLiveData = MutableLiveData<String?>()
    val explainLiveData = MutableLiveData<String?>()//提现说明
    val cardListLiveData = MutableLiveData<MutableList<BankCard>?>()


    /**
     * 提现方式
     */
    fun getWithdrawType() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getWithdrawType()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                walletDrawTypeLiveData.value=null
            }
        }) {
            walletDrawTypeLiveData.value=it
        }
    }
    /**
     * 提现说明
     */
    fun getWithdrawExplain() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getWithdrawExplain()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                explainLiveData.value=null
            }
        }) {
            explainLiveData.value=it
        }
    }


    /**
     * 获取医生钱包余额信息
     */
    fun getDoctorWallet() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getWallet()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                walletLiveData.value = null
            }
        }) {
            walletLiveData.value = it
        }
    }

    /**
     * 获取钱包流水-详情
     */
    fun getWalletDetails(id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getWalletDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                walletDetailsListLiveData.value = null
            }
        }) {
            walletDetailsListLiveData.value = it
        }
    }

    /**
     * 获取医生钱包余额明细
     */
    fun getDoctorWalletDetail(page: Int, type: Int = 0) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getWalletDetail(page = page, pageSize = 10, type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                walletListLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                walletListLiveData.value = null
            } else {
                walletListLiveData.value = it.list
            }
        })

    }


    /**
     * 创建银行卡
     */
    fun addBankCard(addBankCard: AddBankCard) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.addBankCard(addBankCard)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addCardLiveData.value = false
            }
        }, successBlock = {
            addCardLiveData.value = true
            TipsToast.showTips("添加成功")
        })
    }


    /**
     * 创建银行卡
     */
    fun deleteBankCard(id: String) {
        val params = JsonObject().apply {
            addProperty("id",id)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.deleteBankCard(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                deleteCardLiveData.value = false
            }
        }, successBlock = {
            deleteCardLiveData.value = true
        })
    }

    /**
     * 创建银行卡
     */
    fun getCardNumber(id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getCardNumber(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                careNumberLiveData.value=null
            }
        }, successBlock = {
            careNumberLiveData.value=it
        })
    }




    /**
     * 银行卡列表-不分页
     */
    fun getBankCardList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getBankCardList()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                cardListLiveData.value = null
            }
        }, successBlock = {
            cardListLiveData.value = it
        })

    }
}